<template>
  <div class="poster_list">
    <el-row>
      <el-col :span="8"><el-input v-model="wd" placeholder="输入标题关键字搜索" @keyup.enter.native="submitSearch"></el-input></el-col>
      <el-col :span="3" style="margin-left:10px"><el-button type="primary" @click="submitSearch">搜索</el-button></el-col>
    </el-row>
    <el-form :inline="true">
      <el-form-item label="内容性质">
        <el-select v-model="attrId" clearable>
          <el-option value="-1" label="不限"></el-option>
          <el-option v-for="item,n in attrs" :value="item.id" :label="item.name" :key="item.id.toString()"></el-option>
        </el-select>
      </el-form-item>
    </el-form>
    <el-row>
      <el-button icon="plus" @click="$router.push({name: 'EditPoster', params: {id: 'new'}})">新建海报</el-button>
    </el-row>
    <el-table
      v-loading="tbLoading"
      element-loading-text="拼命加载中"
      :data="posterList.list"
      stripe
      class="table"
      @select="handleSelect"
      @select-all="handleSelect">
      <el-table-column type="selection"></el-table-column>
      <el-table-column label="标题" prop="posterName">
        <template slot-scope="scope">
          <a @click="handlePreview(scope.row)" style="cursor:pointer;">{{scope.row.posterName}}</a>
        </template>
      </el-table-column>
      <el-table-column label="内容性质" prop="attributesName" class-name="is-center"></el-table-column>
      <el-table-column label="查看权限" class-name="is-center">
        <template slot-scope="scope">
          <span v-if="scope.row.commonType">公共</span>
          <span v-if="scope.row.commonType&&scope.row.groupType">,</span>
          <span v-if="scope.row.groupType">事业部</span>
          <span v-if="!scope.row.commonType&&!scope.row.groupType">自己可见</span>
          <p v-if="scope.row.auditStatus!==undefined" style="color:#999">{{scope.row.auditStatus===0?'待审核(品牌vi)':scope.row.auditStatus===1?'审核通过':'审核不通过'}}</p>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" class-name="is-center" width="180">
        <template slot-scope="scope">
          {{dateFormat(scope.row.createTime)}}
        </template>
      </el-table-column>
      <el-table-column label="操作" width="150"  class-name="is-center">
        <template slot-scope="scope">
          <el-dropdown>
            <span class="el-dropdown-link">
              操作<i class="el-icon-caret-bottom el-icon--right"></i>
            </span>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item @click.native="$router.push({name:'EditPoster', params: {id: scope.row.id}, query: {cs: 1}})">
                {{userInfo.uid===scope.row.uid ? '编辑' : '使用'}}
              </el-dropdown-item>
              <el-dropdown-item @click.native="handlePreview(scope.row)">预览</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <div class="pagination">
      <el-pagination
        :current-page.sync="page"
        :page-size.sync="size"
        layout="sizes, total, prev, pager, next, jumper"
        :page-sizes="[10, 30, 50, 100]"
        :total="posterList.page.total"
        @current-change="submitSearch()"
        @size-change="handleSizeChange">
      </el-pagination>
    </div>
    <!-- 预览弹层 -->
    <el-dialog title="预览" :visible.sync="dialog" custom-class="dialog_preview">
      <poster-preview :data="preview"></poster-preview>
    </el-dialog>
  </div>
</template>
<script>
  import {mapActions, mapState} from 'vuex'
  import config from '@/config/env'
  import posterPreview from '@/components/common/content/poster/posterPreview'
  export default {
    components: {posterPreview},
    data () {
      return {
        wd: '',
        attrId: '-1',
        auditStatus: '1',
        authStatus: '-1',
        selection: [],
        page: 1,
        size: 10,
        preview: {},
        scale: 1,
        dialog: false,
        tbLoading: false
      }
    },
    mounted () {
      this.getContentTypes()
      this.initPage()
    },
    computed: {
      ...mapState({
        userInfo: state => state.userInfo,
        attrs: state => state.contentManage.contentType,
        posterList: state => state.contentManage.posterList
      }),
    },
    methods: {
      ...mapActions(['getPosterList', 'delPoster', 'getContentTypes']),
      initPage () {
        this.submitSearch()
      },
      // 日期格式化
      dateFormat (date) {
        return this.$date.format(new Date(date), 'YYYY-MM-DD hh:mm:ss')
      },
      async submitSearch () {
        this.tbLoading = true
        await this.getPosterList({way: 2, search: this.wd, state: this.auditStatus, viewPower: this.authStatus, attributes: this.attrId,page: this.page, size: this.size})
        this.tbLoading = false
      },
      async handleDel (id) {
        await this.$confirm('是否确认删除海报模型')
        const res = await this.delPoster(id)
        if (res.data.code === 1) {
          this.$message.success('删除成功')
          this.submitSearch()
        }
      },
      // 预览
      async handlePreview (obj) {
        this.preview = obj
        this.dialog = true
        this.$refs.preview
      },
      handlePreImgLoad () {
        this.scale = (this.$refs.previewImg.naturalWidth) / 400
      },
      // 改变分页大小
      handleSizeChange (size) {
        this.size = size
        this.submitSearch()
      },
      // 选中行
      handleSelect (selection) {
        this.selection = selection
      },
    }
  }
</script>
<style lang="scss">
  .poster_list{
    .el-row{margin-bottom:15px;}
    .dialog_preview{
      &{width:440px;}
    }
  }
</style>
